<?php
namespace app\admin\controller;

use \frame\libs\Session;
use \frame\libs\BaseController;
use \app\admin\model\NoticeModel;

/**
 * 快捷入口
 */
class NoticeController extends BaseController
{ 
    /**
     * 公告管理页
     * @return [type] [description]
     */
    public function index()
    {
        $this->checkPage();
        include VIEW_PATH . 'notice/index.html';
    }

    /**
     * 获取所有公告
     * @return [type] [description]
     */
    public function getNotices()
    {
        $notices = NoticeModel::getInstance()->getNotices();
        foreach ($notices as $key => $value) {
            if ($key==0) {
                $notices[$key]['lastid'] = $notices[$key]['id'];
            }else{
                $notices[$key]['lastid'] = $notices[$key-1]['id'];
            }
            if ($key==count($notices)-1) {
                $notices[$key]['nextid'] = $notices[$key]['id'];
            }else{
                $notices[$key]['nextid'] = $notices[$key+1]['id'];
            }
        }
        echo json_encode($notices);
        return true;
    }

    /**
     * 后台首页获取所有公告
     * @return [type] [description]
     */
    public function getNoticesLimit()
    {
        $notices = NoticeModel::getInstance()->getNoticesLimit();
        echo json_encode($notices);
        return true;
    }

    /**
     * 根据id获取公告
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getNoticeById()
    {
        $notice = NoticeModel::getInstance()->getNoticeById($_POST['id']);
        $notice['expiration_time'] = date('Y-m-d H:i:s', $notice['expiration_time']);
        echo $this->success($notice, "success", 200);
        return true;
    }

    /**
     * 添加公告
     * @return bool [description]
     */
    public function insert()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['sort'] = time();
        $data['expiration_time'] = strtotime($_POST['expiration_time']);
        $data['uid'] = Session::get('uid');
        $data['update_uid'] = Session::get('uid');
        $data['is_deleted'] = 0;
        if (NoticeModel::getInstance()->insertNotice($data)) {
            echo $this->success(null, "添加成功！");
            return true;
        }else{
            echo $this->error(null, "添加失败！");
            return false;
        }
    }

    /**
     * 更新公告
     * @return bool [description]
     */
    public function update()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        $data['expiration_time'] = strtotime($_POST['expiration_time']);
        $data['update_uid'] = Session::get('uid');
        if (NoticeModel::getInstance()->updateNotice($data)) {
            echo $this->success(null, "编辑成功！");
            return true;
        }else{
            echo $this->error(null, "编辑失败！");
            return false;
        }
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function delete()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = array(
           'id' => $_POST['id'],
           'update_time' => time(),
           'update_uid' => Session::get('uid'),
        );
        if (NoticeModel::getInstance()->deleteNoticeById($_POST['id'])) {
            echo $this->success(null, "删除成功！");
            return true;
        }else{
            echo $this->error(null, "删除失败！");
            return false;
        }
    }

    /**
     * 更新置顶
     * @return bool [description]
     */
    public function updateIsTop()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        $data['update_uid'] = Session::get('uid');
        if (NoticeModel::getInstance()->updateIsTop($data)) {
            echo $this->success(null, "设置成功！");
            return true;
        }else{
            echo $this->error(null, "设置失败！");
            return false;
        }
    }

    /**
     * 更新排序
     * @return [type] [description]
     */
    public function updateSort()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        if (!isset($_POST)) {
            echo $this->error(null, "设置失败！");
            return false;
        }
        if ($_POST['id1'] == $_POST['id2']) {
            echo $this->success(null, "设置成功！");
            return true;
        }
        $data = $_POST;
        $data['update_time'] = time();
        $data['update_uid'] = Session::get('uid');
        if (NoticeModel::getInstance()->updateSort($data)) {
            echo $this->success(null, "设置成功！");
            return true;
        }else{
            echo $this->error(null, "设置失败！");
            return false;
        }
    }


}